﻿#include "taskipclient.h"
#include <QDebug>
#include <QMessageBox>

TaskIpClient::TaskIpClient(int index,QObject *parent) : MyObject(parent)
{
    ipdata=globalData->ipList.at(index);
    tcpSocket=new QTcpSocket();
    read=new TcpRead();
}

void TaskIpClient::Start()
{
    timerConnect=new QTimer;
    connect(timerConnect,SIGNAL(timeout()),this,SLOT(dis()));///检测断开重连///
    timerConnect->start(5000);


    timer = new QTimer;
    connect(timer, SIGNAL(timeout()), this, SLOT(FreeInQuiry()));///定时查询///
    timer->start(1000);

    dis();
}

void TaskIpClient::dis()
{
    //tcpSocket->isOpen();
    if(tcpSocket->state() == QAbstractSocket::UnconnectedState)
    {
        if(tcpSocket!=nullptr)
        {
            tcpSocket->close();
            tcpSocket->deleteLater();
            tcpSocket=nullptr;
            tcpSocket=new QTcpSocket(this);
        }
    quint16 port=ipdata->Port;
    tcpSocket->connectToHost(ipdata->IP,port);
    connect(tcpSocket,SIGNAL(connected()),this,SLOT(OnConnected()));///连接成功///


   // connect(globalData,SIGNAL(Tcp()),this,SLOT(OnTimer()));//点击发送
    connect(globalData,SIGNAL(TcpZY()),this,SLOT(OnTimerZY()));///10kw机发送数据///
    connect(globalData,SIGNAL(TcpJX()),this,SLOT(OnTimerJX()));///50kw机发送数据///
    connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(ReadData()));
    connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(disc()));
    connect(this,SIGNAL(critical(QString)),globalData,SLOT(critical(QString)));
    connect(this,SIGNAL(criticalSuccess(QString)),globalData,SLOT(criTicalSuccess(QString)));
    }
}

void TaskIpClient::FreeInQuiry()
{
    if(tcpSocket->state()==QAbstractSocket::ConnectedState)
    {
    QByteArray data;
    data.append((char)0xFF);
    data.append((char)0xC1);
    data.append((char)0x00);
    data.append((char)0x00);
    data.append((char)0x00);
    data.append((char)0xED);
    tcpSocket->write(data);
    }
}

void TaskIpClient::Stop()
{
    timer->stop();
    emit Finished();
}

void TaskIpClient::OnTimerZY()
{
    if(tcpSocket->state()==QAbstractSocket::ConnectedState&&ipdata->Kw=="10Kw")
    {
    QByteArray data;
    data.append((char)0xFF);
    data.append((char)0xC2);
    data.append((char)0x00);
    data.append((char)GetComputerZ(ipdata->Kw));
    data.append((char)GetComputerB(ipdata->Kw));
    data.append((char)GetTower(ipdata->Kw));
    data.append((char)GetTowerB(ipdata->Kw));
    data.append((char)0xED);
    tcpSocket->write(data);
    QString str=data;
    qDebug()<<ipdata->Kw<<"机发送数据："<<str;
    }
}

void TaskIpClient::OnTimerJX()
{
    if(tcpSocket->state()==QAbstractSocket::ConnectedState && ipdata->Kw=="50Kw")
    {
    QByteArray data;
    data.append((char)0xFF);
    data.append((char)0xC2);
    data.append((char)0x00);
    data.append((char)GetComputerZ(ipdata->Kw));
    data.append((char)GetComputerB(ipdata->Kw));
    data.append((char)GetTower(ipdata->Kw));
    data.append((char)GetTowerB(ipdata->Kw));
    data.append((char)0xED);
    tcpSocket->write(data);
    qDebug()<<ipdata->Kw<<"机发送数据："<<QString::fromLocal8Bit(data) ;
    }
}

void TaskIpClient::OnConnected()
{
    qDebug()<<ipdata->Kw<<"机链接成功";
    QByteArray data;
    data.append((char)0xFF);
    data.append((char)0xC1);
    data.append((char)0x00);
    data.append((char)0x00);
    data.append((char)0x00);
    data.append((char)0xED);
    tcpSocket->write(data);
}

void TaskIpClient::ReadData()
{
    QByteArray arr=tcpSocket->readAll();
    qDebug()<<ipdata->Kw<<"机收到数据："<<MyHelper::ToHexString(arr);
    memcpy(read,arr.data(),arr.size());
    if(read->AGREE==0xD1)
    {
        bool tower=true;
        bool computer=true;
        QString Kw;
        if(read->Tower==0x01)
        {
            tower=true;
        }
        else if(read->Tower==0x00)
        {
            tower=false;
        }

        if(read->Computer==0x01)
        {
            computer=true;
        }
        else if(read->Computer==0x00)
        {
            computer=false;
        }

        if(ipdata->Kw=="50Kw")
        {
            Kw="50Kw";
            emit globalData->updateantenna1(Kw,tower,computer);
        }
        else if(ipdata->Kw=="10Kw")
        {
            Kw="10Kw";
            emit globalData->updateantenna2(Kw,tower,computer);
        }
    }
    else if(read->AGREE==0xD2){
        if(read->Reserved==0xEE)
        {
            if(ipdata->Kw=="50Kw")
            {emit globalData->DefeatedJX();}
            else if(ipdata->Kw=="10Kw")
            {emit globalData->DefeatedZY();}
            emit critical(ipdata->Kw);
            return;
        }
        else if(read->Reserved==0x00)
        {

            bool tower=true;
            bool computer=true;
            QString Kw;
            if(read->Tower==0x01)
            {
                tower=true;
            }
            else if(read->Tower==0x00)
            {
                tower=false;
            }

            if(read->Computer==0x01)
            {
                computer=true;
            }
            else if(read->Computer==0x00)
            {
                computer=false;
            }

            if(ipdata->Kw=="50Kw")
            {
                Kw="50Kw";
                emit globalData->updateantenna1(Kw,tower,computer);
            }
            else if(ipdata->Kw=="10Kw")
            {
                Kw="10Kw";
                emit globalData->updateantenna2(Kw,tower,computer);
            }
            emit criticalSuccess(ipdata->Kw);
        }
    }
}


void TaskIpClient::disc()
{
    qDebug()<<ipdata->Kw<<"机断开链接";
}

quint8 TaskIpClient::GetKw(QString Kw)
{
    if(Kw=="50Kw")
    {
        quint8 aa=0x00;
        return aa;
    }
    else if(Kw=="10Kw")
    {
        quint8 bb=0x01;
        return bb;
    }
    else {
        quint8 cc=0xFF;
        return cc;
        qDebug()<<"功率错误！";
    }
}

quint8 TaskIpClient::GetTower(QString Kw)
{
    if(Kw=="50Kw")
    {
        if(globalData->Tower_50==true)
        {
            quint8 aa=0x01;
            return aa;
        }
        if(globalData->Tower_50==false)
        {
            quint8 bb=0x00;
            return bb;
        }
    }
    else if(Kw=="10Kw")
    {
        if(globalData->Tower_10==true)
        {
            quint8 aa=0x01;
            return aa;
        }
        if(globalData->Tower_10==false)
        {
            quint8 bb=0x00;
            return bb;
        }
    }
    else {
        quint8 cc=0xFF;
        return cc;
        qDebug()<<"功率错误！";
    }

}

quint8 TaskIpClient::GetTowerB(QString Kw)
{
    if(Kw=="50Kw")
    {
        if(globalData->Tower_50==true)
        {
            quint8 aa=0x00;
            return aa;
        }
        if(globalData->Tower_50==false)
        {
            quint8 bb=0x01;
            return bb;
        }
    }
    else if(Kw=="10Kw")
    {
        if(globalData->Tower_10==true)
        {
            quint8 aa=0x00;
            return aa;
        }
        if(globalData->Tower_10==false)
        {
            quint8 bb=0x01;
            return bb;
        }
    }
    else {
        quint8 cc=0xFF;
        return cc;
        qDebug()<<"功率错误！";
    }

}

quint8 TaskIpClient::GetComputerZ(QString Kw)//主机状态
{
    if(Kw=="50Kw")
    {
        if(globalData->Computer_50==true)
        {
            quint8 aa=0x01;
            return aa;
        }
        if(globalData->Computer_50==false)
        {
            quint8 bb=0x00;
            return bb;
        }
    }
    else if(Kw=="10Kw")
    {
        if(globalData->Computer_10==true)
        {
            quint8 aa=0x01;
            return aa;
        }
        if(globalData->Computer_10==false)
        {
            quint8 bb=0x00;
            return bb;
        }
    }
    else {
        quint8 cc=0xFF;
        return cc;
        qDebug()<<"功率错误！";
    }

}

quint8 TaskIpClient::GetComputerB(QString Kw)//备机状态
{
    if(Kw=="50Kw")
    {
        if(globalData->Computer_50==true)
        {
            quint8 aa=0x00;
            return aa;
        }
        if(globalData->Computer_50==false)
        {
            quint8 bb=0x01;
            return bb;
        }
    }
    else if(Kw=="10Kw")
    {
        if(globalData->Computer_10==true)
        {
            quint8 aa=0x00;
            return aa;
        }
        if(globalData->Computer_10==false)
        {
            quint8 bb=0x01;
            return bb;
        }
    }
    else {
        quint8 cc=0xFF;
        return cc;
        qDebug()<<"功率错误！";
    }
}

bool TaskIpClient::IsLittleEndian()
{
    union{
        short a;//2字节
        char  b;//1字节
    }test;
    test.a=0x0061;//a
    return test.b == 'a';
}


